package com.wangshidai.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshidai.entity.*;
import com.wangshidai.form.userform.SysUserAssignForm;
import com.wangshidai.form.userform.SysUserForm;
import com.wangshidai.service.SysDeptService;
import com.wangshidai.service.SysUserRoleService;
import com.wangshidai.service.SysUserService;
import com.wangshidai.util.userztreeutil.ListToTree;
import com.wangshidai.util.userztreeutil.impl.ListToTreeImpl;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "用户管理")
@Slf4j
public class UserManageController {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @ApiOperation("查询部门树形列表")
    @GetMapping("/api/department/list1")
    public R getMenuList(){
        List<TreeNode> list = new ArrayList<TreeNode>();
        List<SysDept> list1 = sysDeptService.list();
        for (SysDept sysDept : list1) {
            TreeNode treeNode = new TreeNode();
            BeanUtil.copyProperties(sysDept,treeNode);
            list.add(treeNode);
        }
        ListToTree treeBuilder = new ListToTreeImpl();
        List<TreeNode> tree = treeBuilder.toTree(list);
        System.out.println(tree);
        return new R(200,"查询成功",tree);
    }
    @ApiOperation("根据部门id查询用户列表(用户登录名可模糊查询)")
    @GetMapping("/api/user/list/{currentPage}/{pageSize}/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数"),
            @ApiImplicitParam(name = "id",value = "部门id"),
            @ApiImplicitParam(name = "username",value = "用户名")}
    )
    public R getUserList(@PathVariable("currentPage") Integer c, @PathVariable("pageSize") Integer s, @PathVariable("id") Integer id, String username){
        Page userList = sysUserService.getUserList(c, s, id, username);
        return new R(200,"查询成功",userList);
    }
    @ApiOperation("增加用户")
    @PostMapping("/api/user")
    @CrossOrigin
    public R addUser(@RequestBody SysUserForm userForm){
        log.info("{}",userForm);
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(userForm,sysUser);
        log.info("{}",sysUser);
        Boolean state = sysUserService.saveUser(sysUser);
        if (state){
            return new R(200,"添加成功",null);
        }
        return new R(-1000,"添加失败!手机号 邮箱 或用户名已存在",null);
    }

    @ApiOperation("编辑用户")
    @PutMapping("/api/user")
    @CrossOrigin
    public R updateUser(@RequestBody SysUserForm userForm){
        log.info("{}",userForm);
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(userForm,sysUser);
        log.info("{}",sysUser);
        Boolean state = sysUserService.updateUser(sysUser);
        if (state){
            return new R(200,"修改成功",null);
        }
        return new R(-1000,"修改失败!手机号 邮箱 或用户名已存在",null);
    }
    @ApiOperation(value = "分配角色")
    @PutMapping("/api/user/assingRole")
    @CrossOrigin

    public R assingRole(@RequestBody SysUserAssignForm assignForm){
        log.info("{}",assignForm);
        List<Integer> roleIdList = assignForm.getRoleId();
        if(roleIdList.size()>0){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_id",assignForm.getUserId());
            sysUserRoleService.remove(wrapper);
            Long userId = assignForm.getUserId();
            for (Integer roleId : roleIdList) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(roleId.longValue());
                sysUserRoleService.save(sysUserRole);
            }
            return new R(200,"分配角色成功",null);
        }
        return new R(-1000,"您未选中任何角色",null);
    }
    @ApiOperation("删除用户")
    @PutMapping("api/user/{userId}")
    @ApiImplicitParam(name = "userId",value = "用户id")
    @CrossOrigin
    public R assingRole(@PathVariable Long userId){
        log.info("{}",userId);

        if (userId == null) {
            return new R(-1000,"删除用户失败",null);
        }
        boolean state = sysUserService.removeById(userId);
        if (state) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("user_id",userId);
            sysUserRoleService.remove(wrapper);
            return new R(200,"删除用户成功",null);
        }
        return new R(-1000,",查无此用户,删除用户失败",null);
    }
}
